﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using Manager.Common;
using System.Linq;
using Manager.FormAdd_Edit;
using System.Data.Linq;

namespace Manager.FormMain
{
    public partial class Frm_Barbutton_DanhSachNguoiDung : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();

        public Frm_Barbutton_DanhSachNguoiDung()
        {
            InitializeComponent();
        }

        private void Frm_Barbutton_DanhSachNguoiDung_Load(object sender, EventArgs e)
        {
            db.Refresh(RefreshMode.OverwriteCurrentValues, db.NguoiDungs);
            gridControl1.DataSource = null;
            gridControl1.DataSource = db.NguoiDungs.ToList();
        }

        private void butThemMoi_Click(object sender, EventArgs e)
        {
            frm_AddNewUser frm = new frm_AddNewUser();
            frm.ShowDialog();
        }

        private void butHieuChinh_Click(object sender, EventArgs e)
        {
            var index = gridView1.FocusedRowHandle;
            var nguoiDung = gridView1.GetRow(index) as NguoiDung;
            if (nguoiDung != null)
            {
                frm_ChangePassword frmChangePass = new frm_ChangePassword(nguoiDung);
                frmChangePass.ShowDialog();
                Frm_Barbutton_DanhSachNguoiDung_Load(null, null);
            }
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn xóa tài khoản này?", "Thông báo", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                var index = gridView1.FocusedRowHandle;
                var nguoiDung = gridView1.GetRow(index) as NguoiDung;
                if (nguoiDung != null)
                {
                    db.NguoiDungs.DeleteOnSubmit(nguoiDung);
                    db.SubmitChanges();
                    Frm_Barbutton_DanhSachNguoiDung_Load(null, null);
                }
            }
        }
    }
}